﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NETDB;

public partial class mis_function_user_log : BasePermissionPage
{
  MISEntities dc;
  protected void Page_Load(object sender, EventArgs e)
  {
    dc = new MISEntities();
    if (!IsPostBack)
    {
      ddlYear_txt_end_date.SelectedValue = "2015";
      ddlMonth_txt_end_date.SelectedValue = "12";
    }
  }

  protected void BindGrid()
  {
    SDS_MIS_FUNCTION_USER_LOG.SelectParameters.Clear();

    SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
          " AND (SUBSTR(LOGIN_DATE,0,6) >= '" + ddlYear_txt_start_date.SelectedValue + ddlMonth_txt_start_date.SelectedValue +
          "' AND SUBSTR(LOGIN_DATE,0,6) <= '" + ddlYear_txt_end_date.SelectedValue + ddlMonth_txt_end_date.SelectedValue + "' ) ";

    if (!string.IsNullOrEmpty(txt_loginname_start.Text) && !string.IsNullOrEmpty(txt_loginname_end.Text))
    {
      SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
          " AND (USER_ID >= '" + txt_loginname_start.Text + "' AND USER_ID <= '" + txt_loginname_end.Text + "' ) ";
    }
    else if (!string.IsNullOrEmpty(txt_loginname_start.Text))
    {
      SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
            " AND USER_ID >= '" + txt_loginname_start.Text + "' ";
    }
    else if (!string.IsNullOrEmpty(txt_loginname_end.Text))
    {
      SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
            " AND USER_ID <= '" + txt_loginname_end.Text + "' ";
    }

    switch (ddl_dept.SelectedValue)
    {
      case "0":
        SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
        " AND DEPT_ID IN (SELECT BI4101 FROM AS400_BIF41) ";
        break;
      case "1":
        SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
        " AND DEPT_ID IN (SELECT A.BI4101 FROM AS400_BIF41 A LEFT JOIN AS400_ZDF306 B ON B.F30600 = A.BI4101 WHERE A.BI4101 = A.BI4119) ";
        break;
      case "2":
        SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
        " AND DEPT_ID IN (SELECT A.BI4101 FROM AS400_BIF41 A LEFT JOIN AS400_ZDF306 B ON B.F30600 = A.BI4101 WHERE A.BI4101 = A.BI4115) ";
        break;
      case "3":
        SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
        " AND DEPT_ID IN (SELECT A.BI4101 FROM AS400_BIF41 A LEFT JOIN AS400_ZDF306 B ON B.F30600 = A.BI4101 WHERE A.BI4101 = A.BI4114) ";
        break;
      case "4":
        SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
        " AND DEPT_ID IN (SELECT A.BI4101 FROM AS400_BIF41 A LEFT JOIN AS400_ZDF306 B ON B.F30600 = A.BI4101 WHERE A.BI4101 = A.BI4113) ";
        break;
      case "5":
        SDS_MIS_FUNCTION_USER_LOG.SelectCommand +=
        " AND DEPT_ID IN (SELECT A.BI4101 FROM AS400_BIF41 A LEFT JOIN AS400_ZDF306 B ON B.F30600 = A.BI4101 WHERE A.BI4101 = A.BI4112) ";
        break;
    }

    if (ddl_menu.SelectedValue != "0")
    {
      SDS_MIS_FUNCTION_USER_LOG.SelectCommand += " AND FUNCTION_ID='" + ddl_menu.SelectedValue + "'";
    }

    SDS_MIS_FUNCTION_USER_LOG.SelectCommand += " ORDER BY FUNCTION_ID";
    SDS_MIS_FUNCTION_USER_LOG.Select(DataSourceSelectArguments.Empty);

    gv_login_log.DataBind();
  }

  protected void btn_search_Click(object sender, EventArgs e)
  {
    try
    {
      var controlItem = new List<ControlItem>() 
      {
        new ControlItem("ddlYear_txt_start_date", ddlYear_txt_start_date.SelectedValue, ddlYear_txt_start_date.GetType()),
        new ControlItem("ddlMonth_txt_start_date", ddlMonth_txt_start_date.SelectedValue, ddlMonth_txt_start_date.GetType()),
        new ControlItem("ddlYear_txt_end_date", ddlYear_txt_end_date.SelectedValue, ddlYear_txt_end_date.GetType()),
        new ControlItem("ddlMonth_txt_end_date", ddlMonth_txt_end_date.SelectedValue, ddlMonth_txt_end_date.GetType()),
        new ControlItem("ddl_menu", ddl_menu.SelectedValue, ddl_menu.GetType()),
        new ControlItem("ddl_dept", ddl_dept.SelectedValue, ddl_dept.GetType()),
        new ControlItem("txt_loginname_start", txt_loginname_start.Text, txt_loginname_start.GetType()),
        new ControlItem("txt_loginname_end", txt_loginname_end.Text, txt_loginname_end.GetType())
      };
      ConditionRecordUtil.RecordSearchConditionData(controlItem);
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayInfoMessage(ex.Message, lbl_message);
    }

    Button btn = sender as Button;
    if (btn.ID == "btn_export")
    {
      if (gv_login_log.Rows.Count == 0)
      {
        CommonUtil.DisplayInfoMessage("無資料可匯出!", lbl_message);
        return;
      }
    }

    BindGrid();

    if (gv_login_log.Rows.Count == 0)
    {
      CommonUtil.DisplayErrorMessage("查無資料", lbl_message);
    }

    if (btn.ID == "btn_export")
    {
      if (gv_login_log.Rows.Count > 0)
      {
        gv_login_log.Columns[5].Visible = false; // 隱藏明細欄位
        ExportUtil.ExportGridViewToExcel(DateTime.Now.ToString("yyyyMMdd-hhmmss-") + "FunctionLog", gv_login_log);
      }
    }
  }

  protected void gv_login_log_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      Label lbl_date = e.Row.FindControl("lbl_LOGIN_DATE") as Label;
      Label lbl_user = e.Row.FindControl("lbl_USER_ID") as Label;
      Label lbl_user_name = e.Row.FindControl("lbl_user_name") as Label;
      var user = dc.MIS_USER_AP.Where(u => u.USER_ID == lbl_user.Text).SingleOrDefault();
      string username = string.Empty;
      if (user != null)
      {
        username = user.USER_NAME;
      }
      lbl_user_name.Text = lbl_user.Text + " [" + username.Trim() + "] ";
      lbl_date.Text = lbl_date.Text.Substring(0, 4) + "/" + lbl_date.Text.Substring(4, 2);

      Label lbl_deptid = e.Row.FindControl("lbl_DEPT_ID") as Label;
      Label lbl_deptname = e.Row.FindControl("lbl_DEPT_NAME") as Label;
      var dept = dc.MIS_DEPT_AP
          .Where(d => d.DEPT_ID == lbl_deptid.Text)
          .SingleOrDefault();
      if (dept != null)
      {
        lbl_deptname.Text = dept.DEPT_NAME;
      }
      Label lbl_function = e.Row.FindControl("lbl_FUNCTION_ID") as Label;
      Label lbl_function_name = e.Row.FindControl("lbl_FUNCTION_NAME") as Label;
      var fun = dc.MIS_FUNCTION_AP.Where(f => f.FUNCTION_ID == lbl_function.Text).SingleOrDefault();
      if (fun != null)
      {
        lbl_function_name.Text = fun.FUNCTION_NAME;
      }
    }
  }

  public string UserID { get; set; }
  public string UserName { get; set; }
  public string FunctionID { get; set; }
  public string FunctionName { get; set; }
  public string FunctionDate { get; set; }
  public string DeptName { get; set; }

  protected void gv_login_log_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {
    Label lbl_user = gv_login_log.Rows[e.NewSelectedIndex].FindControl("lbl_USER_ID") as Label;
    UserID = lbl_user.Text;
    Label lbl_user_name = gv_login_log.Rows[e.NewSelectedIndex].FindControl("lbl_user_name") as Label;
    UserName = lbl_user_name.Text;
    Label lbl_FUNCTION_ID = gv_login_log.Rows[e.NewSelectedIndex].FindControl("lbl_FUNCTION_ID") as Label;
    FunctionID = lbl_FUNCTION_ID.Text;
    Label lbl_FUNCTION_NAME = gv_login_log.Rows[e.NewSelectedIndex].FindControl("lbl_FUNCTION_NAME") as Label;
    FunctionName = lbl_FUNCTION_NAME.Text;
    Label lbl_date = gv_login_log.Rows[e.NewSelectedIndex].FindControl("lbl_LOGIN_DATE") as Label;
    FunctionDate = lbl_date.Text;
    Label lbl_dept = gv_login_log.Rows[e.NewSelectedIndex].FindControl("lbl_DEPT_NAME") as Label;
    DeptName = lbl_dept.Text;
    Server.Transfer("~/Management/System/mis_function_user_log_detail.aspx");
  }


  public override void VerifyRenderingInServerForm(Control control)
  {

  }
  protected void SDS_MIS_FUNCTION_USER_LOG_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
  {
    if (!Page.IsPostBack)
    {
      e.Cancel = true;
    }
  }
  protected void gv_login_log_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    gv_login_log.PageIndex = e.NewPageIndex;
    BindGrid();
  }
}